{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d1f20875-c50d-46df-906b-49ec1d0c6002",
   "metadata": {},
   "source": [
    "## 10.2 实现多通道图像卷积计算\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c90a18b9-d8e8-431c-856c-35f1e4ed05e1",
   "metadata": {},
   "source": [
    "### 1.任务描述\n",
    "\n",
    "假设有一张图像，其大小为5×5×3，像素矩阵如下："
   ]
  },
  {
   "cell_type": "raw",
   "id": "fda35add-808b-436a-a224-ea33268deb25",
   "metadata": {},
   "source": [
    "[[[0,1,0],[2,0,1],[2,2,2],[0,1,2],[2,1,0]],\n",
    "[[1,1,0],[1,2,1],[1,2,1],[1,1,0],[1,0,0]],\n",
    "[[0,1,2],[0,1,2],[1,0,0],[2,2,2],[2,1,1]],\n",
    "[[0,0,0],[0,0,1],[1,0,1],[2,1,0],[2,1,2]],\n",
    "[[0,2,1],[2,1,2],[2,1,2],[2,2,1],[0,0,1]]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61c78769-b47f-46fa-a721-5656f1cd3e3f",
   "metadata": {},
   "source": [
    "有一3×3×3的卷积核，其像素矩阵如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d02a8fcd-1a2e-4833-9912-857ab407159c",
   "metadata": {},
   "outputs": [],
   "source": [
    "[[[-1,1,-1],[1,0,-1],[0,1,-1]],\n",
    "[[-1,1,0],[0,1,1],[1,0,0]],\n",
    "[[-1,1,0],[1,0,1],[-1,1,1]]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3903b806-5cb1-4796-b785-eb466876dec8",
   "metadata": {},
   "source": [
    "要求：\n",
    "\n",
    "- 请使用卷积核对图像进行卷积计算，并输出计算结果。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f5b4fc39-cbcf-432a-bf1e-e75e642d4b87",
   "metadata": {},
   "source": [
    "### 2.知识准备\n",
    "\n",
    "见教程。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "55043130-4496-43a3-803b-9bc1cea8b1b8",
   "metadata": {},
   "source": [
    "### 3.任务分析构\n",
    "\n",
    "对于彩色二维图像的卷积计算，同样可以使用tf.nn.conv2d方法。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "435c6090-cfda-4f46-a550-22a368e41e4a",
   "metadata": {},
   "source": [
    "### 4.任务实施\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ec75eb6c-5da3-467d-a471-ca3b47242dd6",
   "metadata": {},
   "source": [
    "执行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2ae9da58-e339-4d22-9f8d-ca255711d89e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "input形状: (1, 5, 5, 3)\n",
      "卷积核形状: (3, 3, 3, 1)\n",
      "输出形状: (1, 3, 3, 1)\n",
      "输出：\n",
      " [[9. 6. 4.]\n",
      " [8. 6. 9.]\n",
      " [7. 8. 5.]]\n"
     ]
    }
   ],
   "source": [
    "import  tensorflow as tf\n",
    "from  tensorflow  import  keras\n",
    "from PIL import Image\n",
    "import numpy as np\n",
    "# 1，定义输入\n",
    "# 原始输入\n",
    "input=tf.constant([[[0,1,0],[2,0,1],[2,2,2],[0,1,2],[2,1,0]],\n",
    "                      [[1,1,0],[1,2,1],[1,2,1],[1,1,0],[1,0,0]],\n",
    "               [[0,1,2],[0,1,2],[1,0,0],[2,2,2],[2,1,1]],\n",
    "               [[0,0,0],[0,0,1],[1,0,1],[2,1,0],[2,1,2]],\n",
    "                  [[0,2,1],[2,1,2],[2,1,2],[2,2,1],[0,0,1]]],dtype=tf. float32)\n",
    "# 进行形状转换（增加批维度）\n",
    "input=tf.expand_dims(input,0)\n",
    "print(\"input形状:\",input.shape)\n",
    "\n",
    "# 2，定义卷积核\n",
    "# 维度格式：[filter_height, filter_width, in_channels, out_channels] \n",
    "filters=tf.constant([[[-1,1,-1],[1,0,-1],[0,1,-1]],\n",
    "                        [[-1,1,0],[0,1,1],[1,0,0]],\n",
    "                        [[-1,1,0],[1,0,1],[-1,1,1]]],dtype=tf.float32)\n",
    "# 进行形状转换（增加输出通道维度）\n",
    "# 使(3,3,3)变为(3,3,3,1)\n",
    "filters=tf.expand_dims(filters,3)\n",
    "print(\"卷积核形状:\",filters.shape)\n",
    "# 3，卷积计算\n",
    "out=tf.nn.conv2d(   \n",
    "    # 输入\n",
    "    input=input,   \n",
    "    # 过滤器（卷积核）\n",
    "    filters=filters,\n",
    "    # 滑动步长\n",
    "    strides=1,\n",
    "    padding='VALID')\n",
    "# 4，加偏置项\n",
    "b=tf.constant(1,dtype=tf.float32)\n",
    "out=out+b\n",
    "print(\"输出形状:\",out.shape)\n",
    "# 删除长度为1的维度\n",
    "out=tf.squeeze(out)\n",
    "print(\"输出：\\n\",out.numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6044c99-0741-4378-b2b6-f60c293cc3a9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
